var topicIndex = getQueryString('topicID')
let dom = localStorage.getItem('dom')
$('.inputFormTwo').html(dom)

// 保存事件
$('.save').click(function () {
    let data = []
    for (let i = 1; i <= topicIndex; i++) {
        if ($('#topicNum' + i).text() !== '') {
            // 定义一个空数组来存储选中的值
            let arr = []
            // 题号
            let topicNum = $('#topicNum' + i).parent().siblings('.index-num').attr('data-order')
            // 题目类型
            let topicType = $('#topicNum' + i).attr('data-type')
            // 是否必填
            let required = $('#topicNum' + i).parent().siblings('.must').attr('data-id')
            // 选中input的值
            if(topicType == '1'){
                let inputName = $("input[name='topic"+i+"']:checked").val()
                if(inputName){
                    arr.push(inputName)
                }
            } else if(topicType == '2'){
                $("input[name='topic"+i+"']:checked").each(function(){
                    arr.push($(this).val())
                })
            } else if(topicType == '3'){
                let wendaName = $('#wenda' + i).val()
                arr.push(wendaName)
            } else if(topicType == '4'){
                let starNum = $('.star' + i).attr('data-star')
                arr.push(starNum)
            }
            let obj = {
                "topicID":i,
                "qmxid": topicNum,
                "answer": arr.join(),
                "answerType": topicType,
                "isRequired": required
            }
            data.push(obj)

            if(required == '2' && arr.length == 0){
                alert('题目'+i+'为必填项')
            }
        
        }
    }
    console.log(data)


    localStorage.setItem('data',JSON.stringify(data))
    window.location.href = './save.html'
})

// 编辑回显事件
$('.edit').click(function(){
    let dom22 = $('.inputFormTwo').html()
    localStorage.setItem('dom22',dom22)
    window.location.href = './index.html?topicIndex='+topicIndex;
})


// 获取url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}












